jQuery(function($){
	var supplierSettlement={
			grid : null,
			init:function(){
				var exportExcel = [];
				var items = [];			
				exportExcel = [{text: "导出EXCEL",
					click: function(){
						supplierSettlement.exportExcel();
					}, icon: "up"},{ line: true }];
				items = items.concat(exportExcel);
				toolbar = {items : items};
				
				$("#btnSearch").on("click",function(){
					//supplierSettlement.pageList();
					supplierSettlement.grid.set({
						parms: [
								{name: "settlementNo", value:$("#settlementNo").val()},
								{name: "supplierNo", value:$("#supName").val()},
								{name: "checkState", value:$("#checkState").val()},
								{name: "settlementStartTime", value:$("#settlementStartTime").val()},
								{name: "settlementEndTime", value:$("#settlementEndTime").val()}
			                    ],
						url: "supplierSettlementPageList.do"      
					});
					//supplierSettlement.grid.loadData();					
				});
				var supplierConfig={
						url:"/base/supplierInfo/supplierList.do",
						dom:$("#supName"),
						value:"code",
						text:"supName"
				};
				this.getSuppliers(supplierConfig);
				this.checkState=[
				    {value:0,text:"未审核"},
				    {value:1,text:"已审核"},
				    {value:2,text:"审核不通过"}
				];	
				this.setSelectBox(this.checkState,$("#checkState"));
				this.pageList();
			},
			pageList:function(){
				supplierSettlement.grid = $("#maingrid").ligerGrid({
		            columns: [
		            /*{ display: '结算id', name: 'settlementId'},*/     
		            { display: '结算编号', name: 'settlementNo'},
		            /*{ display: '供应商编号', name: 'supplierNo'},*/
				    { display: '供应商名称', name: 'supplierName'},
		            { display: '结算总金额(元)', name: 'settlementTotal',render:
		            	function (rowdata, rowindex, value){			            	
	            			return value?parseFloat(value).toFixed(2):"";
            			} 
		            },
		            { display: '结算开始日期', name: 'startDate', width:'160'},
		            { display: '结算结束日期', name: 'endDate', width:'160'},
		            /*{ display: '结算周期', name: 'accountTime'},*/
		            { display: '结算时间', name: 'settlementTime',width:'160'},
		            { display: '审核状态', name: 'checkState', render:
		            	function (rowdata, rowindex, value){			            	
		            		return supplierSettlement.getTextByValue(supplierSettlement.checkState,value);
	            		} 
		            },
		            { display: '审核时间',width:'160', name: 'checkTime'},
		            { display: '操作',width:'20%', isSort: false, render: 
		            	function (rowdata, rowindex, value){
			            	var check = "<a href='javascript:toCheckSettlementInfo(" + rowindex + ",1)'>审核</a> ";
			            	var detail = "<a href='javascript:showSettlementInfo(" + rowindex + ",1)'>详情</a> ";
			            	if(rowdata.checkState){
			            		check="";
			            	}
		            		return check+detail;
		            	}
		            }], 
		            pageSize:30,
		            width: '100%',
		            height: '100%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [
							{name: "settlementNo", value:$("#settlementNo").val()},
							{name: "supplierNo", value:$("#supplierNo").val()},
							{name: "checkState", value:$("#checkState").val()},
							{name: "settlementStartTime", value:$("#settlementStartTime").val()},
							{name: "settlementEndTime", value:$("#settlementEndTime").val()}
		                    ],
					url: "supplierSettlementPageList.do",
					toolbar: toolbar
		        });
				grid=supplierSettlement.grid;
			},
			setSelectBox:function(objs,dom){
				if(objs  && objs.length){
					dom.html('<option value="">请选择</option>');
					for(var x in objs){
						dom.append('<option value="'+objs[x].value+'">'+objs[x].text+'</option>');
					}
				}
			},
			getTextByValue:function(objs,value){
				if(objs && objs.length){
					for(var x in objs){
						if(objs[x].value==value){
							return objs[x].text;
						}
					}
				}
			},
			getSuppliers:function(config){
				$.ajax({
					url:basePath+config.url,
					type:"post",
					dataType:"json",
					success:function(result){
						if(result  && result.length){
							config.dom.html('<option value="">请选择</option>');
							for(var x in result){
								config.dom.append('<option value="'+result[x][config.value]+'">'+result[x][config.text]+'</option>');
							}
						}
					}
				});
			},
			exportExcel:function(){
				var exportUrl = basePath + "/financing/settlement/exportSupplierSettlement.do";
				var params = [{name: "settlementNo", value:$("#settlementNo").val()},
							  {name: "supplierNo", value:$("#supName").val()},
							  {name: "checkState", value:$("#checkState").val()},
						      {name: "settlementStartTime", value:$("#settlementStartTime").val()},
							  {name: "settlementEndTime", value:$("#settlementEndTime").val()}
		                     ];
	            var form = $("<form>");   //定义一个form表单
	            form.attr('style', 'display:none');   //在form表单中添加查询参数
	            form.attr('target', '');
	            form.attr('method', 'post');
	            form.attr('action', exportUrl);
	            for(var i=0;i<params.length;i++){
	            	var input = $('<input>');
		            input.attr('type', 'hidden');
		            input.attr('name', params[i].name);
		            input.attr('value', params[i].value);
		            form.append(input);   //将查询参数控件提交到表单上
	            }
	            $('body').append(form);  //将表单放置在web中 
	            form.submit();
	            form.remove();
			}
	};
	supplierSettlement.init();
}); 